package com.xmg.p2p.base.controller;

import com.xmg.p2p.base.domain.Account;
import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.domain.UserFile;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.mapper.IplogMapper;
import com.xmg.p2p.base.query.UserFileQueryObject;
import com.xmg.p2p.base.service.ILogininfoService;
import com.xmg.p2p.base.service.RealAuthService;
import com.xmg.p2p.base.service.UserFileService;
import com.xmg.p2p.base.service.UserinfoService;
import com.xmg.p2p.base.service.impl.IplogServiceimpl;
import com.xmg.p2p.base.util.BidConst;
import com.xmg.p2p.base.util.JSONResult;
import com.xmg.p2p.base.util.RequireLogin;
import com.xmg.p2p.base.util.UserContext;
import com.xmg.p2p.business.domain.BidRequest;
import com.xmg.p2p.business.query.BidRequestQueryObject;
import com.xmg.p2p.business.service.IBidRequestService;
import javafx.scene.chart.PieChart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @BelongsProject: jinrong-website
 * @BelongsPackage: com.xmg.p2p.base.controller
 * @Author: Andy
 * @CreateTime: 2020--11--25  09:14
 * @Description: 登录控制器
 */
@Controller
public class LoginController {
    @Autowired
    private ILogininfoService logininfoService;
    @Autowired
    private IBidRequestService bidRequestService;
//    @Autowired
//    private UserinfoService userinfoService;
//    @Autowired
//    private RealAuthService realAuthService;
//    @Autowired
//    private UserFileService userFileService;

    /**
     * 登录的信息
     * @param username
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public JSONResult login(String username, String password, HttpServletRequest request) {
        JSONResult json = new JSONResult();
        Logininfo login = this.logininfoService.login(username, password, request.getRemoteAddr(),Logininfo.USER_CLIENT);
        if (login==null){
            json.setSuccess(false);
            json.setMsg("用户名或密码错误!");
        }
        return json;
    }

    /**
     * 跳转到首页并在首页展示发标的信息
     * @param model 模型
     * @return
     */
    @RequestMapping("/index")
    public String toMain(Model model){
        if (UserContext.getCurrent() != null){
            BidRequestQueryObject qo = new BidRequestQueryObject() ;
            /*设置贷款对象的状态信息*/
            qo.setStates(new int[] { BidConst.BIDREQUEST_STATE_BIDDING,//招标中
                    BidConst.BIDREQUEST_STATE_PAYING_BACK ,//还款中
                    BidConst.BIDREQUEST_STATE_COMPLETE_PAY_BACK}) ; //已还清
            /*设置按哪一列排序*/
            qo.setOrderBy("bidRequestState") ; //按照借款的状态排序
            /*设置按升序排列*/
            qo.setOrderType("ASC");
            qo.setPageSize(5);
            model.addAttribute("bidRequests" ,this.bidRequestService.queryForList(qo));
        }else{
            model.addAttribute("bidRequests" ,null);
        }
        return "main";
    }


//    @RequestMapping("borrow_info")
//    public String borrowInfoDetail(Long id,Model model){
//        //根据借款对象id获取借款对象
//        BidRequest bidRequest = this.bidRequestService.getById(id);
//        //获得当前的借款人
//        Userinfo userinfo = this.userinfoService.getById(bidRequest.getCreateUser().getId());
//        model.addAttribute("bidRequest",bidRequest);
//        model.addAttribute("userInfo",userinfo);
//        //查询当前标的 历史审核对象
//        model.addAttribute("audits", this.bidRequestService.listAuditHistorayByBidRequest(bidRequest.getId()));
//        //用户的实名认证信息
//        model.addAttribute("realAuth", this.realAuthService.get(userinfo.getRealAuthId()));
//
//        //借款人所有的相关的风控材料(已经审核通过)
//        UserFileQueryObject qo = new UserFileQueryObject() ;
//        qo.setState(UserFile.STATE_AUDIT);
//        qo.setApplierId(userinfo.getId());
//        qo.setPageSize(-1); //为了检索从某一个偏移量到记录集的结束所有的记录行，可以指定第二个参数为 -1：
//        model.addAttribute("userFiles", this.userFileService.queryForList(qo));
//        return "borrow_info";
//    }
}
